package com.kantboot.system.user.balance.handle;

import com.kantboot.system.user.balance.domain.entity.SysUserBalanceChangeRecord;
import com.kantboot.system.user.balance.service.ISysBalanceService;
import com.kantboot.timer.event.GloberTimerEvent;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component
public class BalanceChangeHandle {

    @Resource
    private ISysBalanceService service;

    /**
     * 余额变化事件
     * @param event 事件
     */
    @EventListener
    public void onApplicationEvent(GloberTimerEvent event) {
        log.info("余额变化处理中...");
        // 获取所有没有处理的余额变化记录
        List<SysUserBalanceChangeRecord> userBalanceChangeRecordByNotProcessed = service.getUserBalanceChangeRecordByNotProcessed();
        for (SysUserBalanceChangeRecord record : userBalanceChangeRecordByNotProcessed) {
            // 处理余额变动
            service.handleBalanceChangeRecord(record.getId());
        }
        log.info("余额变化处理结束");
    }

}
